﻿using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web.Mvc;
using Autofac;
using Flurl.Http;
using Pay.Core;
using Pay.DTO;
using Pay.DTO.tenPay;
using Pay.IService;
using Pay.TO;

namespace Pay.Web.Areas.Admin.Controllers
{
    [RouteArea("admin")]
    [RoutePrefix("charge")]
    public class ChargeController : AdminController
    {
        private IComponentContext _iocContext;
        private IMerchantService _iMerchantService;
        private IChargeService _iChargeService;
        private IPayHtmlService _iPayHtmlService;

        public ChargeController(IComponentContext iocContext, IMerchantService iMerchantService, IChargeService iChargeService, IPayHtmlService iPayHtmlService)
        {
            _iocContext = iocContext;
            _iMerchantService = iMerchantService;
            _iChargeService = iChargeService;
            _iPayHtmlService = iPayHtmlService;
        }

        public ActionResult Index()
        {
            return View();
        }

        public JsonResult Gets(ChargeQuery query)
        {
            var model = _iChargeService.Get(query);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        public JsonResult Get(long id)
        {
            var model = _iChargeService.Get(id);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 单条支付查询
        /// </summary>
        /// <returns>The first.</returns>
        /// <param name="merchant_no">O单商商户号</param>
        /// <param name="order_no">Order no.</param>
        /// <param name="pay_channel">Pay channel.</param>
        [Route("charge_first")]
        public async Task<JsonResult> ChargeFirst(string merchant_no, string order_no, PayChannel pay_channel)
        {
            var iPayService = _iocContext.ResolveKeyed<IPayService>(pay_channel);
            var query = new refund_model_query { merchant_no = merchant_no, method = "5003", nonce = DtoHelper.GuidTo16String(), order_no = order_no, pay_channel = pay_channel };
            var result = await iPayService.QuickPayQuery(query);
            return Json(ResultToJson.Json(result.result_msg, result.status), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 支付通知
        /// </summary>
        /// <returns>The first.</returns>
        /// <param name="merchant_no">Merchant no.</param>
        /// <param name="order_no">Order no.</param>
        /// <param name="pay_channel">Pay channel.</param>
        [Route("charge_notice")]
        public async Task<JsonResult> ChargeNotice(string merchant_no, string order_no, PayChannel pay_channel)
        {
            var iPayService = _iocContext.ResolveKeyed<IPayService>(pay_channel);
            var result = await iPayService.Notify(merchant_no, order_no);
            if (result == "OK")
            {
                return Json(ResultToJson.Json("通知成功", ""), JsonRequestBehavior.AllowGet);
            }
            return Json(ResultToJson.Json<string>(result), JsonRequestBehavior.AllowGet);
        }

        [Route("get_htmls")]
        public JsonResult GetHtmls(PayHtmlQuery query)
        {
            var model = _iPayHtmlService.Get(query);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 代付
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("html_first")]
        public async Task<JsonResult> ChargeHtmlFirst(string id)
        {
            var model = _iPayHtmlService.Get(id);
            var data = model.html.ToObject<payment_data>();
            var pay_notify = new ten_sub_payment();
            pay_notify.amount = data.amount;
            pay_notify.rateAmount = data.rateAmount;
            pay_notify.custBankAccountNo = data.account_no;
            pay_notify.notifyUrl = id;
            pay_notify.custPhone = data.mobile;
            pay_notify.custID = data.id_card;
            pay_notify.custName = data.account_name;
            string resultString = await "http://pay.timebiz.cn/pay/ten_sub_re_pay".PostJsonAsync(pay_notify).ReceiveString();
            var result = resultString == "success" ? ResultToJson.Json("代付成功") : ResultToJson.Json<string>("代付失败");
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [Route("html_del_choose")]
        public JsonResult ChargeHtmlDeletes(PayHtmlQuery query)
        {
            _iPayHtmlService.Delete(query);
            return Json(ResultToJson.Json("删除成功", ""), JsonRequestBehavior.AllowGet);
        }

        [Route("html_dels")]
        public JsonResult ChargeHtmlDelete(List<string> id)
        {
            _iPayHtmlService.Delete(id.ToArray());
            return Json(ResultToJson.Json("删除成功", ""), JsonRequestBehavior.AllowGet);
        }
    }
}